package com.sockect3d.data;

import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Random;

import struct.JavaStruct;
import struct.LEDataInputStream;
import struct.LEDataOutputStream;
import struct.StructException;

/**
 * Created by Jaqen on 2015/11/27 NetConnection
 */
public class NetConnection
{
	private Socket socket;
	private final String SERVER_HOST_IP = "city24.live-earth.cn";
//    private final String SERVER_HOST_IP = "10.0.5.37";

	/* 服务器端口 */
	private final int SERVER_HOST_PORT = 5002;
	private LEDataInputStream inputStream;
	private LEDataOutputStream outputStream;
	private byte[] headerData;
	private byte[] headData = new byte[30];
	public boolean isConnected()
	{
		return socket.isConnected();
	}

	public NetConnection(String addr, int port, boolean isNoTimeout)
			throws IOException, StructException
	{
		socket = new Socket(addr, port);
		socket.setKeepAlive(false);

		if (isNoTimeout)
			socket.setSoTimeout(0);
		outputStream = new LEDataOutputStream(socket.getOutputStream());
		inputStream = new LEDataInputStream(socket.getInputStream());
		headerData = JavaStruct.pack(new StructProtocolHeader());
	}
	byte[] tok = null;
	public NetConnection()
	{
		Random rd1 = new Random();
		int n = rd1.nextInt(1000000000);
		long x = n;
		byte[] token = DataUtils.longToBytes(x);
		tok = token;

	}
	public void connec() throws IOException, StructException {
		socket = new Socket(SERVER_HOST_IP, SERVER_HOST_PORT);
		socket.setKeepAlive(false);
		outputStream = new LEDataOutputStream(socket.getOutputStream());
		inputStream = new LEDataInputStream(socket.getInputStream());
		headerData = JavaStruct.pack(new StructProtocolHeader());
		if(isConnected()){
			ctlShowVideod();
		}else{
			Log.d("----链接失败","sssssss");
		}
	}
	public void ctlShowVideod(){
		ArrayList<DList_da> dList = new ArrayList<DList_da>();
		dList.add(new DList_da(793));
		DLInfo_da DLInfo = new DLInfo_da(1, 4,-1 ,dList);
		ImagWH wh = new ImagWH();
		wh.setImageWidth(600);
		wh.setImageHeight(372);
		Vidio_can v_id = new Vidio_can(456, 2, 1, 2000, wh, DLInfo);
		Gson gson = new Gson();
		String requestJson = gson.toJson(v_id);
		Log.d("===========",requestJson);
		try
		{
			sendData(
					requestJson.getBytes(),
					NetProtocol.HMS_COMMUNICATION_PROTOCOL_OPERATOR_TYPE_ROUTER,
					NetProtocol.HMS_COMMUNICATION_PROTOCOL_OPERATOR_CODE_ROUTER_3D,
					tok, "imgwd");

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	public void setNonTimeout()
	{
		try
		{
			socket.setSoTimeout(0);
		} catch (SocketException e)
		{
			e.printStackTrace();
		}
	}
	public void sendDatag(byte[] data, long operatorType, long operatorCode,
						  byte[] token,String code) throws StructException, IOException
	{

		byte[] allData;
		StructProtocolHeader header = new StructProtocolHeader();

		header.setOperatorTypeL(operatorType);
		header.setOperatorCodeL(operatorCode);

		token = DataUtils.longToBytes(0);
		Random rd1 = new Random();
		long n = rd1.nextLong();

		header.xhTokenGuid = DataUtils.longToBytes(n);
		if (data != null)
		{
			header.setPacketSizeI(data.length);
			headerData = JavaStruct.pack(header);
			allData = DataUtils.combineBytesData(headerData, data);
		} else
		{
//			Log.d("---------------------------------------", "data为空咯噢噢噢噢哦哦哦"+operatorCode);
			headerData = JavaStruct.pack(header);
			allData = DataUtils.combineBytesData(headerData, null);;
		}

		outputStream.write(allData);
		outputStream.flush();
//		Log.d("-------------------", data.length+"噢噢噢噢哦哦哦"+allData.length+"----"+operatorCode+code);
	}
	public void sendData(byte[] data, long operatorType, long operatorCode,
						 byte[] token,String code) throws StructException, IOException
	{

		byte[] allData;
		StructProtocolHeader header = new StructProtocolHeader();

		header.setOperatorTypeL(operatorType);
		header.setOperatorCodeL(operatorCode);
		header.setwVersion(1);
//		token = DataUtils.longToBytes(0);

//		long n = rd1.nextLong();

		header.xhTokenGuid = token;
		if (data != null)
		{
			header.setPacketSizeI(data.length);
			headerData = JavaStruct.pack(header);
			allData = DataUtils.combineBytesData(headerData, data);
		} else
		{
//			Log.d("---------------------------------------", "data为空咯噢噢噢噢哦哦哦"+operatorCode);
			headerData = JavaStruct.pack(header);
			allData = DataUtils.combineBytesData(headerData, null);;
		}

		outputStream.write(allData);
		outputStream.flush();
//		Log.d("-------------------", data.length+"噢噢噢噢哦哦哦"+allData.length+"----"+operatorCode+code);
	}

	public void searchImage(byte[] data, long operatorType, long operatorCode) throws StructException, IOException
	{

		byte[] allData;
		SearchImageHeader header = new SearchImageHeader();

		header.setOperatorTypeL(operatorType);
		header.setOperatorCodeL(operatorCode);


		if (data != null)
		{
			header.setPacketSizeI(data.length);
			headerData = JavaStruct.pack(header);
			allData = DataUtils.combineBytesData(headerData, data);
		} else
		{
			headerData = JavaStruct.pack(header);
			allData = DataUtils.combineBytesData(headerData, null);;
		}

		outputStream.write(allData);
		outputStream.flush();
	}

	public void sendHeat(byte[] data, long operatorType, long operatorCode,
						 byte[] token,String code) throws StructException, IOException
	{

		byte[] allData;
		StructProtocolHeader header = new StructProtocolHeader();

		header.setOperatorTypeL(operatorType);
		header.setOperatorCodeL(operatorCode);
		header.setwVersion(2);
//		token = DataUtils.longToBytes(0);

//		long n = rd1.nextLong();

		header.xhTokenGuid = token;
		if (data != null)
		{
			header.setPacketSizeI(data.length);
			headerData = JavaStruct.pack(header);
			allData = DataUtils.combineBytesData(headerData, data);
		} else
		{
//			Log.d("---------------------------------------", "data为空咯噢噢噢噢哦哦哦"+operatorCode);
			headerData = JavaStruct.pack(header);
			allData = DataUtils.combineBytesData(headerData, null);;
		}

		outputStream.write(allData);
		outputStream.flush();
//		Log.d("-------------------", data.length+"噢噢噢噢哦哦哦"+allData.length+"----"+operatorCode+code);
	}
	public StructProtocolHeader receiveHeader() throws IOException,
            StructException
	{
		StructProtocolHeader header = new StructProtocolHeader();
//		headerData = JavaStruct.pack(header);
//		Log.d("-------------------", header.getPacketSizeI()+"---123456789读取了"+headerData.length);
//		byte[] arr = new byte[4];
//		inputStream.readFully(arr);
		inputStream.readFully(headData);
//		Log.d("-------------------", "读取了吗无语了");
		JavaStruct.unpack(header, headData);

//		Log.d("-------------------","读取了没"+header.getPacketSizeI()+"-p--"+header.getOperatorCodeL()+"---"+header.getOperatorTypeL()+"==="+header.getwHeader());
		return header;
	}
	public SearchImageHeader receiveImageHeader() throws IOException,
            StructException
	{
		SearchImageHeader header = new SearchImageHeader();
//		headerData = JavaStruct.pack(header);
//		Log.d("-------------------", header.getPacketSizeI()+"---123456789读取了"+headerData.length);
//		byte[] arr = new byte[4];
//		inputStream.readFully(arr);
		inputStream.readFully(headData);
//		Log.d("-------------------", "读取了吗无语了");
		JavaStruct.unpack(header, headData);

//		Log.d("-------------------","读取了没"+header.getPacketSizeI()+"-p--"+header.getOperatorCodeL()+"---"+header.getOperatorTypeL()+"==="+header.getwHeader());
		return header;
	}
	public void receiveData(byte[] data) throws IOException
	{
		if(data!=null){
			Log.d("------", "读取中。。。。。");
			inputStream.readFully(data);
			Log.d("---", "读取完成");
		}
	}

	public void close() throws IOException
	{
		inputStream.close();
		outputStream.close();
		socket.close();
	}
}
